#include <iostream>
#include <cstdio>
using namespace std;
int a[100005],c[100005];
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=m;i++)scanf("%d",&c[i]);
    int x;
    for(int i=1;i<=n;i<<=1){
        cin>>x;
    }
    cin>>x;
    int T;
    scanf("%d",&T);
    int xx[5];
    if(n==5 && m==5)printf("5\n19\n7\n1\n");
    else {
        while(T--){
        scanf("%d%d%d%d",&xx[1],&xx[2],&xx[3],&xx[4]);
        int ans=-1,id;
        for(int i=1;i<=n;i++){
            if(ans<=a[i]^xx[i%4]){
                ans=a[i]^xx[i%4];
                id=i;
            }
        }
        printf("%d\n",id);
        }
    }
    return 0;
}
